# Maintainer: Andrea Zagli <andrea.zagli.free@gmail.com>

_realname=meson
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.55.2
pkgrel=1
pkgdesc="High-productivity build system (mingw-w64)"
arch=('any')
url="https://mesonbuild.com/"
license=("Apache 2")
options=('strip' 'staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-setuptools"
         "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/mesonbuild/${_realname}/releases/download/${pkgver}/${_realname}-${pkgver}.tar.gz"
        'color-term.patch'
        '0002-Default-to-sys.prefix-as-the-default-prefix.patch'
        '0003-Strip-the-prefix-from-all-paths-when-installing-with.patch'
        '0004-fix-gtk-doc.patch'
        'install-man.patch')
sha256sums=('78a3f4ee3864a8fce9f028d38d900ef997fd398b851765e35a0185e5c01b11f7'
            '5805aed0a117536eb16dd8eef978c6be57c2471b655ede63e25517c28b4f4cf0'
            '363182db7e7059526353278966aa4704e8a26cbaf7c3b7dc5d6e4f01692a40e6'
            '2093c617cf3146a4cea601e7c73400d8ec5fd52ac5cf642c4f5af2d6493b1cb1'
            'ca14c322dd18e7c0614fda1ec8ba42ba5148305e641612e0290e4a350c042adf'
            '0682a36cb75e545a78b81293303835a16171f25baf949905dc08029436efff84')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "${srcdir}"/color-term.patch
  patch -Np1 -i "${srcdir}"/0002-Default-to-sys.prefix-as-the-default-prefix.patch
  patch -Np1 -i "${srcdir}"/0003-Strip-the-prefix-from-all-paths-when-installing-with.patch
  patch -Np1 -i "${srcdir}"/0004-fix-gtk-doc.patch
  patch -Np1 -i "${srcdir}"/install-man.patch
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -rf "${srcdir}"/${_realname}-${pkgver}/. "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}"/build-${CARCH}

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py \
    install --prefix=${MINGW_PREFIX} --root="${pkgdir}"

  PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}/bin/||g" \
    -i "${pkgdir}${MINGW_PREFIX}/bin/meson-script.py"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
